package com.vs.model;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;

public class UploadForm extends ActionForm {

	private static final long serialVersionUID = 2025885369419610335L;

	private FormFile uploadFile = null;
	private Integer projectId;
	private String docType = null;
	
	// Getters, setters and reset methods are omitted for making the code short.

	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		System.out.println("validate called");
		if (uploadFile.getFileSize() == 0)
			errors.add("uploadedFile", new ActionMessage(
					"errors.file.notselected"));
		if (errors.isEmpty()) {
			if (!uploadFile.getContentType().equalsIgnoreCase("image/jpeg")
					&& !uploadFile.getContentType().equalsIgnoreCase(
							"image/jpg")
					&& !uploadFile.getContentType().equalsIgnoreCase(
							"image/pjpeg"))
				errors.add("uploadedFile", new ActionMessage(
						"errors.file.type", uploadFile.getFileName(),
						uploadFile.getContentType()));
			if (uploadFile.getFileSize() > 5000000)
				errors.add("uploadedFile", new ActionMessage(
						"errors.file.size", uploadFile.getFileName(),
						uploadFile.getFileSize()));
		}
		
		
		return errors;
	}


	public FormFile getUploadFile() {
		return uploadFile;
	}


	public void setUploadFile(FormFile uploadFile) {
		this.uploadFile = uploadFile;
	}


	public Integer getProjectId() {
		return projectId;
	}
	
	public void setProjectId(Integer projectId) {
		this.projectId = projectId;
	}
	
	public String getDocType() {
		return docType;
	}
	
	public void setDocType(String docType) {
		this.docType = docType;
	}
}
